package com.oneday.ashop.webgate.base.file;

import com.oneday.ashop.core.base.util.DateUtil;
import com.oneday.ashop.core.entity.CommonConstant;
import com.oneday.ashop.webgate.base.ftp.SFtpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ImageFilter;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.Iterator;

/**
 * 基础文件操作类
 * 这里用ftp用于上传和下载
 * User:bob.yang
 * Date 2015/8/18 0018
 */
@Slf4j
public class BaseFileOperate {

    //图片基本路径
    public static final String IMG_BASE_URL = "http://139.129.12.155:8089/";

    /**
     *  截取图片
     * @param imgUrl 图片地址
     * @param x 起始横坐标
     * @param y 起始纵坐标
     * @param width 宽度
     * @param height 高度
     * @param imgType 扩展名
     * @return
     */
    public static String cropImg(String imgUrl, int x, int y, int width, int height, String imgType) throws MalformedURLException {
        URL url;
        InputStream is;
        String newImgUrl = null;
        try {
            //从http读入图片的数据流
            url = new URL(imgUrl);
            is = url.openStream();
            Iterator iterator = ImageIO.getImageReadersByFormatName(imgType);
            ImageReader reader = (ImageReader)iterator.next();
            ImageInputStream iis = ImageIO.createImageInputStream(is);
            reader.setInput(iis, true);
            ImageReadParam param = reader.getDefaultReadParam();
            Rectangle rect = new Rectangle(x, y, width, height);
            param.setSourceRegion(rect);
            BufferedImage bi = reader.read(0,param);
            ByteArrayOutputStream bs = new ByteArrayOutputStream();
            ImageOutputStream imOut;
            imOut = ImageIO.createImageOutputStream(bs);
            ImageIO.write(bi, imgType, imOut);
            is= new ByteArrayInputStream(bs.toByteArray());
            String name = getRandomImgName(imgType);
            SFtpUtil.upload(is, name, 1);
            newImgUrl = IMG_BASE_URL + name;
        } catch (MalformedURLException e) {
            log.error("{}:路径不合法！", imgUrl, e);
            throw new MalformedURLException();
        } catch (IOException e) {
            log.error("图片：{}处理的过程中发生了网络异常！", imgUrl, e);
            throw new MalformedURLException();
          }
        return newImgUrl;
}

    /**
     * 生成一个图片名称
     * @param imgType 格式
     * @return
     */
    public static String getRandomImgName(String imgType) {
        String name = DateUtil.format(new Date(), "yyyyMMddHHmmssSSS");
        name = name + (int)Math.random() * 100 + "." + imgType;
        return name;
    }

    /**
     * 图片上传
     */
    public static String imageUpload(MultipartFile file) throws IOException {
        String oldImageName = file.getOriginalFilename();
        int index = oldImageName.lastIndexOf('.');
        String imgType = oldImageName.substring(index + 1);
        String newImageNmae = getRandomImgName(imgType);
        SFtpUtil.upload(file, newImageNmae,  CommonConstant.IMAGE_PATH);
        return IMG_BASE_URL + newImageNmae;
    }

    /**
     * 文本文件上传
     */
    public static void fileUpload() {

    }

    /**
     * 文本文件下载
     */
    public static void fileDownload() {

    }
}
